package com.imau.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //处理请求乱码
        req.setCharacterEncoding("UTF-8");
        //req获取参数GET POST
        System.out.println(req.getMethod());
        System.out.println(req.getRequestURI());
        System.out.println(req.getRequestURL());
        System.out.println(req.getHeader("user-agent"));
        System.out.println(req.getQueryString());
        //获取GET请求参数所有
        System.out.println(req.getParameter("username"));//获取GET\POST请求参数所有
        System.out.println(req.getParameter("userpass"));//获取GET\POST请求参数所有

        System.out.println(req.getParameter("hobby"));//获取GET\POST请求参数所有

        //处理一个参数用多个值使用getParameterValues
        String[] hobbies = req.getParameterValues("hobby");

        System.out.println("hobbies" + Arrays.asList(hobbies));
        //数据查询
        BufferedReader reader = req.getReader();
        String s = reader.readLine();
        System.out.println(s);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}